package Hashtable;

import java.util.HashMap;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class canConstruct {
    //不使用map使用数组也可以
    public boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            map.put(magazine.charAt(i), map.getOrDefault(magazine.charAt(i), 0) + 1);
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            map.put(ransomNote.charAt(i), map.getOrDefault(ransomNote.charAt(i), 0) - 1);
        }
        return !map.values().stream().anyMatch(value -> value < 0);
    }
}
